//
//  BBDefaultController.m
//  xNose
//
//  Created by ben smith on 9/7/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBDefaultController.h"


@implementation BBDefaultController

BBDefaultController* sharedDefaultController;

@synthesize layoutManager;
@synthesize currencyFormatter;
@synthesize percentFormatter;

#define BB_FONT_STARTSIZE 12
// this is all a bit hacky.  
// TODO: move all these over to some editable prefs system instead of just hardcoding

-(void)awakeFromNib
{
	sharedDefaultController = self;
	
	// these are the 'localization strings' , it would be nice to do this the right way
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBCostString"] == nil)
		[[NSUserDefaults standardUserDefaults] setObject:@"Cost" forKey:@"BBCostString"];
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBIncomeString"] == nil)
		[[NSUserDefaults standardUserDefaults] setObject:@"Income" forKey:@"BBIncomeString"];
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBProfitString"] == nil)
		[[NSUserDefaults standardUserDefaults] setObject:@"Profit" forKey:@"BBProfitString"];

	// these control the radial interface, the max value is how many units per revolution
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBMaxRadialValue"] == nil)
		[[NSUserDefaults standardUserDefaults] setFloat:720.0 forKey:@"BBMaxRadialValue"];
	// the round value is what all the value displays round their numbers to
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBRadialRoundValue"] == nil)
		[[NSUserDefaults standardUserDefaults] setFloat:100.0 forKey:@"BBRadialRoundValue"];
	
	// this is the container size
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBContainerWidth"] == nil)
		[[NSUserDefaults standardUserDefaults] setFloat:240.0 forKey:@"BBContainerWidth"];

	// this is the container size
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBFontStartSize"] == nil)
		[[NSUserDefaults standardUserDefaults] setFloat:12.0 forKey:@"BBFontStartSize"];
	
	// include the 'grand total' view
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBShowGrandTotals"] == nil)
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"BBShowGrandTotals"];

	// include the 'grand total' view
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBCanEditNodes"] == nil)
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"BBCanEditNodes"];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBLaunchInFullscreen"] == nil)
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"BBLaunchInFullscreen"];
	
	
	
	// include the 'grand total' view
	if ([[NSUserDefaults standardUserDefaults] objectForKey:@"BBLibraryButtonPosition"] == nil)
		[[NSUserDefaults standardUserDefaults] setFloat:0.5 forKey:@"BBLibraryButtonPosition"];
	
	// we are going to abuse the standard user defaults for now
	// make a nice new default props dictionary
	[[NSUserDefaults standardUserDefaults] setFloat:35.0 forKey:@"BBTitleBarHeight"];
	[[NSUserDefaults standardUserDefaults] setFloat:4.0 forKey:@"BBBorderWidth"];
	
	[[NSUserDefaults standardUserDefaults] setFloat:25.0 forKey:@"BBInfoButtonSize"];
	[[NSUserDefaults standardUserDefaults] setFloat:35.0 forKey:@"BBChartButtonSize"];
	[[NSUserDefaults standardUserDefaults] setFloat:55.0 forKey:@"BBBigChartButtonSize"];
	[[NSUserDefaults standardUserDefaults] setFloat:30.0 forKey:@"BBTriangleButtonSize"];

	[[NSUserDefaults standardUserDefaults] setFloat:25.0 forKey:@"BBRadialControlSize"];
	
	[[NSUserDefaults standardUserDefaults] setFloat:55.0 forKey:@"BBCollectionHeight"];
	[[NSUserDefaults standardUserDefaults] setFloat:38.0 forKey:@"BBLeafHeight"];
	[[NSUserDefaults standardUserDefaults] setFloat:1.0 forKey:@"BBLeafBorder"];
	[[NSUserDefaults standardUserDefaults] setFloat:6.0 forKey:@"BBLeafHeightInset"];
	[[NSUserDefaults standardUserDefaults] setFloat:40.0 forKey:@"BBConnectionInset"];

	[[NSUserDefaults standardUserDefaults] setFloat:20.0 forKey:@"BBDisclosureOffset"];

	[[NSUserDefaults standardUserDefaults] setFloat:35.0 forKey:@"BBLeftInsetWidth"];
	
	[[NSUserDefaults standardUserDefaults] setFloat:10.0 forKey:@"BBRightInsetWidth"];

	[[NSUserDefaults standardUserDefaults] setFloat:20.0 forKey:@"BBTextInsetWidth"];

	[[NSUserDefaults standardUserDefaults] setFloat:10.0 forKey:@"BBRoundedRadius"];
	[[NSUserDefaults standardUserDefaults] setFloat:4.0 forKey:@"BBSmallRoundedRadius"];

	[[NSUserDefaults standardUserDefaults] setFloat:12.0 forKey:@"BBLeafFontSize"];
	[[NSUserDefaults standardUserDefaults] setFloat:14.0 forKey:@"BBCollectionFontSize"];
	[[NSUserDefaults standardUserDefaults] setFloat:16.0 forKey:@"BBTitleBarFontSize"];

	[[NSUserDefaults standardUserDefaults] setFloat:55.0 forKey:@"BBRightTabOffset"];

	[[NSUserDefaults standardUserDefaults] setObject:@"MyriadPro-Regular" forKey:@"BBRegularFontFace"];
	[[NSUserDefaults standardUserDefaults] setObject:@"MyriadPro-Bold" forKey:@"BBBoldFontFace"];
	
	self.layoutManager = [[NSLayoutManager alloc] init]; // memory leak?
	currencyFormatter = [[NSNumberFormatter alloc] init];
	[currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_0];
	[currencyFormatter setFormat:@"#,##0 €"];
	[currencyFormatter usesGroupingSeparator];
	[currencyFormatter setLocalizesFormat:YES];
	
	percentFormatter = [[NSNumberFormatter alloc] init];
	[percentFormatter setFormatterBehavior:NSNumberFormatterBehavior10_0];
	[percentFormatter setFormat:@"#0.## %"];
	[percentFormatter usesGroupingSeparator];
	[percentFormatter setLocalizesFormat:YES];
	
	
	
}

// this is for the file buttons
-(IBAction)browseForFile:(id)sender
{

	NSString * key;
	if ([sender tag] == 1) key = @"BBScene1FileName";
	if ([sender tag] == 2) key = @"BBScene2FileName";
	if ([sender tag] == 3) key = @"BBScene3FileName";
	if ([sender tag] == 4) key = @"BBExternalAppLocation";
	
	int result;
	NSOpenPanel *oPanel = [NSOpenPanel openPanel];
	
	[oPanel setAllowsMultipleSelection:NO];
	result = [oPanel runModalForDirectory:NSHomeDirectory()
																	 file:nil types:nil];
	if (result == NSOKButton) {
		NSArray *filesToOpen = [oPanel filenames];
		NSString *aFile = [filesToOpen objectAtIndex:0];
		[[NSUserDefaults standardUserDefaults] setObject:aFile forKey:key];
	}
}

+(NSNumberFormatter*)currencyFormatter
{
	return sharedDefaultController.currencyFormatter;
}

+(NSNumberFormatter*)percentFormatter
{
	return sharedDefaultController.percentFormatter;
}


+(float)fontHeight:(NSFont*)aFont
{
	return [sharedDefaultController.layoutManager defaultLineHeightForFont:aFont];
}



// a whole slew of handy methods for getting fonts
+(NSFont*)fontWithSize:(NSInteger)size key:(NSString*)key
{
	NSString * name = [[NSUserDefaults standardUserDefaults] objectForKey:key];
	return [NSFont fontWithName:name size:size];
}

+(NSFont*)infoFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart key:@"BBRegularFontFace"];
}

+(NSFont*)regularSmallFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 2 key:@"BBRegularFontFace"];
}

+(NSFont*)regularMediumFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 4 key:@"BBRegularFontFace"];
}

+(NSFont*)regularLargeFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 6 key:@"BBRegularFontFace"];
}

+(NSFont*)titleFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 10 key:@"BBRegularFontFace"];
}


+(NSFont*)boldSmallFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 2 key:@"BBBoldFontFace"];
}

+(NSFont*)boldMediumFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 4 key:@"BBBoldFontFace"];
}

+(NSFont*)boldLargeFont
{
	int fontStart = [[NSUserDefaults standardUserDefaults] integerForKey:@"BBFontStartSize"];
	return [BBDefaultController fontWithSize:fontStart + 6 key:@"BBBoldFontFace"];
}



// some handy color methods
+(NSColor*)costColor
{
	return [NSColor colorWithDeviceRed:186.0/255.0 green:27.0/255.0 blue:54.0/255.0 alpha:1.0];
}

+(NSColor*)incomeColor
{
	return [NSColor colorWithDeviceRed:228.0/255.0 green:159.0/255.0 blue:40.0/255.0 alpha:1.0];
}

+(NSColor*)profitColor
{
	return [NSColor colorWithDeviceRed:37.0/255.0 green:121.0/255.0 blue:110.0/255.0 alpha:1.0];
}


/*
 "MyriadPro-Black",
 "MyriadPro-BlackCond",
 "MyriadPro-BlackCondIt",
 "MyriadPro-BlackIt",
 "MyriadPro-BlackSemiCn",
 "MyriadPro-BlackSemiCnIt",
 "MyriadPro-BlackSemiExt",
 "MyriadPro-BlackSemiExtIt",
 "MyriadPro-Bold",
 "MyriadPro-BoldCond",
 "MyriadPro-BoldCondIt",
 "MyriadPro-BoldIt",
 "MyriadPro-BoldSemiCn",
 "MyriadPro-BoldSemiCnIt",
 "MyriadPro-BoldSemiExt",
 "MyriadPro-BoldSemiExtIt",
 "MyriadPro-Cond",
 "MyriadPro-CondIt",
 "MyriadPro-It",
 "MyriadPro-Light",
 "MyriadPro-LightCond",
 "MyriadPro-LightCondIt",
 "MyriadPro-LightIt",
 "MyriadPro-LightSemiCn",
 "MyriadPro-LightSemiCnIt",
 "MyriadPro-LightSemiExt",
 "MyriadPro-LightSemiExtIt",
 "MyriadPro-Regular",
 "MyriadPro-SemiCn",
 "MyriadPro-SemiCnIt",
 "MyriadPro-SemiExt",
 "MyriadPro-SemiExtIt",
 "MyriadPro-Semibold",
 "MyriadPro-SemiboldCond",
 "MyriadPro-SemiboldCondIt",
 "MyriadPro-SemiboldIt",
 "MyriadPro-SemiboldSemiCn",
 "MyriadPro-SemiboldSemiCnIt",
 "MyriadPro-SemiboldSemiExt",
 "MyriadPro-SemiboldSemiExtIt",
 
 
 */


@end
